package com.xy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xy.entity.GateExceptionalEntity;
import com.xy.request.GateExceptionalRequest;
import com.xy.request.HeartbeatRequest;
import com.xy.response.GateResponse;
import com.xy.response.HeartbeatResponse;
import com.xy.service.IGateExceptionalService;
import com.xy.service.IInspectBaseService;
import com.xy.vo.InitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.redis.lock.RedisLock;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 核验辅助相关类
 */
@RestController
@AllArgsConstructor
@RequestMapping("inspect-base")
@Slf4j
@Api(value = "核验辅助管理", tags = "核验辅助管理")
public class InspectBaseController extends BladeController {
	private final IInspectBaseService iInspectBaseService;
	private final IGateExceptionalService gateExceptionalService;

	/**
	 * 获取初始化配置
	 *
	 * @param venueCode
	 * @return
	 */
	@GetMapping("/init")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取初始化配置", notes = "传入 initializationDTO的 venueId")
	public R<InitVO> detail(String venueCode) {
		return data(iInspectBaseService.getInit(venueCode));
	}

	/**
	 * 闸机保存
	 */
	@GetMapping("/gata-save")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "闸机保存", notes = "传入 deviceDTO")
	@RedisLock(value = "blade:lock:gata:save",waitTime = 0)
	public R<GateResponse> gataSave(String gateRef) {
		return data(iInspectBaseService.gataSave(gateRef));
	}

	/**
	 * 闸机关联位置
	 */
	@GetMapping("/position-gate-rel")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "闸机关联位置", notes = "deviceDTO")
	public R<GateResponse> positionGateRel(String gateRef, String positionCode) {
		return data(iInspectBaseService.positionGateRel(gateRef, positionCode));
	}

	/**
	 * 心跳获取
	 */
	@PostMapping("/get-heartbeatl")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "心跳获取", notes = "deviceDTO")
	public R<HeartbeatResponse> getHeartbeatl(@RequestBody HeartbeatRequest heartbeatRequest) {
		return data(iInspectBaseService.getHeartbeat(heartbeatRequest));
	}

	/**
	 * 闸机异常保存
	 *
	 * @param gateExceptionalRequest
	 * @return
	 */
	@PostMapping("/gate-exception")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "闸机异常保存", notes = "传入DeviceExceptionalDTO")
	public R deviceException(@Valid @RequestBody GateExceptionalRequest gateExceptionalRequest) {
		GateExceptionalEntity exceptional = BeanUtil.copy(gateExceptionalRequest, GateExceptionalEntity.class);
		return R.status(gateExceptionalService.save(exceptional));
	}

	/**
	 * 版本更新更新回传
	 *
	 * @param
	 * @return
	 */
	@PostMapping("/version-post-back")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "版本更新更新回传", notes = "传入VersionUpdateDTO ")
	public R versionUpdateBackhaul() {
		return R.status(true);
	}

}
